<?php

//------------------------------------------------------------------------------
// Function to show information about the Event & Member Attendance
//
//   $arrayEvent = Array containing all information about the event
//   $arrayMemberAttendance = Array containing all information about the event
//	returns echo the Event Info, Member Attendance Info, Form RSVP
//------------------------------------------------------------------------------
/* ****
function showInfoEventMemberAttendance($arrayEvent, $arrayMemberAttendance) {

    showInfoEvent($arrayEvent);
    
    echo '<div class="timingEventsLineOuter"></div>';

    showInfoMemberAttendance($arrayMemberAttendance);

    echo '<p></p>';
    
    if (strcasecmp($arrayEvent['Status'], 'b') == 0)
    {
        showRSVPForm($arrayEvent['EventId'], 0, FALSE);
    }
    else
    {
        // TODO: Check for auto date block - RSVP Form
        showRSVPForm($arrayEvent['EventId'], MAX_NUM_ALLOW_GUESTS, TRUE);
    }

    echo '<p class="eDescriptionUpcoming"><hr /></p>';
    


}
 **** */


//------------------------------------------------------------------------------
// Function to show information about the Upcoming Events or Details Event
//
//   $arrayEvent = Array containing all information about the event
//   $memberInfo = Information about the Member
//   $timingEvent = What is the Event (U: Upcoming, S: Short Version, D: Detail)
//	returns echo the Event Info
//------------------------------------------------------------------------------
function showIndexEvents($arrayEvent, $memberInfo, $timingEvent) {

    echo '<div class="tablelessEvent">';
    echo    '<div class="blockTable">';

    echo        '<div class="blockTable">';

    echo            '<div class="leftColEventDisplay">';
    echo                '<div class="boxContainerEvent">';
    echo                    '<p class="eTitle">' . $arrayEvent['Title'] . '</p>';
    echo                    '<p class="eDate">' . changeSQLDateEventDisplay($arrayEvent['Date']) . ' - ' . $arrayEvent['Time'] . '</p>';
    echo                    '<p class="eSpeaker">Speaker: </p>';
    echo                    '<p class="eSpkrName">' . $arrayEvent['SpeakerName'] . '</p>';
    echo                    '<p class="eSpkrInfo">' . $arrayEvent['SpeakerInfo'] . '</p>';
    echo                    '<p class="eVenue">Venue: </p>';
    echo                    '<p class="eVenueName">' . $arrayEvent['VenueName'] . '</p>';
    echo                    '<p class="eVenueInfo">' . $arrayEvent['VenueInfo'] . '</p>';
    
    if (strcasecmp($timingEvent,'U') == 0)
    {
        echo                    '<p class="eRSVPorLogin">' . messageToRSVPorLogin($memberInfo) . '</p>';
    }

    echo                '</div>';
    echo            '</div>';

    echo            '<div class="rightColEventDisplay">';
    echo                '<div class="boxContainerEvent">';
    echo                    '<img class="ePicture" align="right" src="' . findImageNameExtensionEvent($arrayEvent['EventId']) . '" alt="Event Picture" />';
    echo                '</div>';
    echo            '</div>';

    echo        '</div>';

    echo        '<div class="boxContainerEvent">';
    echo             '<p class="eDescription">' . formatDescriptionEvent($arrayEvent['Description'], $timingEvent);

    if (strcasecmp($timingEvent,'S') == 0)
    {
        echo                                    '<a href="EventDetail.php?eventID=' . $arrayEvent['EventId'] . '" title="More Details">&nbspMore Details</a>';
    }

    echo             '</p>';
    echo        '</div>';

    echo    '</div>';

    echo '</div>';

}


//------------------------------------------------------------------------------
// Function to show information about Member Attendance & RSVP Form
//
//   $arrayEvent = Array containing all information about the event
//   $arrayMemberAttendance = Array containing all information about the event
//	returns echo Member Attendance Info, Form RSVP
//------------------------------------------------------------------------------
function showMemberAttendanceRSVPForm($arrayEvent, $arrayMemberAttendance) {

    echo '<div class="tablelessEvent">';
    echo        '<div class="blockTable">';

    echo            '<div class="leftColEventDisplay">';
    echo                '<div class="boxRSVPLeft">';


    // Printing the information of Member Attendance
    if(empty($arrayMemberAttendance))
    {
        echo                '<p class="entrybodybold"><font color="#FF1F1F">You have not RSVP for the Upcoming Event yet.</font> <br />Please select one of the following options to RSVP.</p>';
    }
    else
    {
        switch(strtolower($arrayMemberAttendance['Attendance']))
        {
            case 'y':
                $stringGuest = 'out guest';
                if ($arrayMemberAttendance['Guests'] > 0)
                {
                    if ($arrayMemberAttendance['Guests'] == 1)
                    {
                        $stringGuest = ' 1 guest';
                    } else {
                        $stringGuest = ' ' . $arrayMemberAttendance['Guests'] . ' guests';
                    }
                }
                echo        '<p class="entrybodybold"><font color="#00CC00">You are Attending with' . $stringGuest .  '.</font> <br />You can change your attendance by selecting one of the following options.</p>';

                break;

            case 'n':
                echo        '<p class="entrybodybold"><font color="#FF8A14">You are NOT attending.</font> <br />You can change your attendance by selecting one of the following options.</p>';
                break;

            case 'p':
            default:
                echo        '<p class="entrybodybold"><font color="#FF1F1F">You have not RSVP for the Upcoming Event yet.</font> <br />Please select one of the following options to RSVP.</p>';
                break;
        }
    }

    echo                '</div>';
    echo            '</div>';

    echo            '<div class="rightColEventDisplay">';
    echo                '<div class="boxRSVPRight">';

    // Printing RSVP Form
    if (isEventOpenToRSVP($arrayEvent))
    {
        echo              '<form id="rsvpF" name="rsvpF" method="POST" action="RSVPExec.php ">';
        echo              '<fieldset class="rsvpForm">';

        echo                '<div align="center">
                                <input type="submit" name="NoAttending" value="Not Attending" style="width:170px"/>
                            </div>';

        echo                '<div align="center">
                                <input type="submit" name="Attending0" value="Attending without Guest" style="width:170px"/>
                            </div>';

        for ($index = 0; $index < getMaxGuestEventRSVP($arrayEvent); $index++)
        {
            if ($index == 0)
            {
                echo        '<div align="center">
                                <input type="submit" name="Attending1" value="Attending with 1 Guest" style="width:170px"/>
                            </div>';
            }
            else
            {
                echo        '<div align="center">
                                <input type="submit" name="Attending' . ($index+1) .'" value="Attending with ' . ($index+1) . ' Guests" style="width:170px"/>
                            </div>';
            }
        }

        echo                '<input type="hidden" name="EventId" value="' . $arrayEvent['EventId'] .'" />';

        echo              '</fieldset>';
        echo              '</form>';
    }
    else
    {
        echo                '<p class="entrybodybold"><font color="#FF1F1F">
                                We are Sorry. This event is currently BLOCKED for RSVP. If you require further assistance, please contact one of the Excutives.
                             </font></p>';
    }


    echo                '</div>';
    echo            '</div>';

    echo        '</div>';
    echo '</div>';
}



//------------------------------------------------------------------------------
// Function to determine if an event is open to RSVP or Blocked
//
//   $arrayEvent = Array containing all information about the event
//	returns true: Event open to registration
//              false: Event close to registration or Error
//------------------------------------------------------------------------------
function isEventOpenToRSVP($arrayEvent) {

    if (strcasecmp($arrayEvent['Status'],'U') == 0)
    {
        $arraySQLEventDate = explode("-", $arrayEvent['Date']); // [0]: Year, [1]: Month . [2]: Day
        $dateEventPHP = mktime(0, 0, 0, $arraySQLEventDate[1], $arraySQLEventDate[2], $arraySQLEventDate[0] );

        $dateForClosingRSVP = time() - ( MAX_EVENTS_HOURS_BLOCK_RSVP * 60 * 60);

        if ($dateEventPHP >= $dateForClosingRSVP)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return FALSE;
    }
}

//------------------------------------------------------------------------------
// Function to determine the numbee of guests allowed to invite by RSVP
//
//   $arrayEvent = Array containing all information about the event
//	returns true: The max number of guests
//------------------------------------------------------------------------------
function getMaxGuestEventRSVP($arrayEvent) {

        switch(strtolower($arrayEvent['Type']))
        {
            case 'a':
                return MAX_EVENTS_GUESTS_AGM;
                break;

            case 'w':
                return MAX_EVENTS_GUESTS_WHIFFETTE;
                break;

            case 'r':
            default:
                return MAX_EVENTS_GUESTS_REGULAR;
                break;
        }
}




//------------------------------------------------------------------------------
// Function to show information about the Event
//
//   $arrayEvent = Array containing all information about the event
//	returns echo the Event Info
//------------------------------------------------------------------------------
/* ****
function showInfoEvent($arrayEvent) {
    echo '<p><p id="eTitleUpcoming">' . $arrayEvent['Title'] . '</p>';
    echo '<p id="eDateUpcoming">' . changeSQLDateEventDisplay($arrayEvent['Date']) . ' - ' . $arrayEvent['Time'] . '</p>';
    echo '<p id="eSpkrNameUpcoming">Speaker:&nbsp&nbsp&nbsp&nbsp&nbsp' . $arrayEvent['SpeakerName'] . '</p>';
    echo '<p id="eSpkrInfoUpcoming">' . $arrayEvent['SpeakerInfo'] . '</p>';
    echo '<p id="eVenueNameUpcoming">Location:&nbsp&nbsp&nbsp&nbsp&nbsp' . $arrayEvent['VenueName'] . '</p>';
    echo '<p id="eVenueInfoUpcoming">' . $arrayEvent['VenueInfo'] . '</p>' ;
    echo '<p id="eFeeUpcoming">Members Fee: $' . $arrayEvent['FeeMember'] . ' - Guest Fee: $' . $arrayEvent['FeeGuest'] . '</p>';
    echo '<img src="' . findImageNameExtensionEvent($arrayEvent['EventId']) . '" alt="Event Picture" /></p>';
    echo '<p id="eDescriptionUpcoming">' . nl2br($arrayEvent['Description']) . '</p>';
    
    //TODO: DISPLAY PICTURE IN NICE WAY
    //TODO: FIX THE DESCRIPTION DISPLAY NICE
    // splitLongTextMaxWords(nl2br($arrayEvent['Description']),100)
    

}
**** */

//------------------------------------------------------------------------------
// Function to show information about Member Attendance
//
//   $arrayMemberAttendance = Array containing all information about the event
//	returns echo Member Attendance Info
//------------------------------------------------------------------------------
/* ****
function showInfoMemberAttendance($arrayMemberAttendance) {

    if(empty($arrayMemberAttendance))
    {
        echo '<p class="entrybodybold"><font color="#FF1F1F">You have not RSVP yet. Please select one of the following options to RSVP.</font></p>';
    }
    else
    {
        switch(strtolower($arrayMemberAttendance['Attendance']))
        {
            case 'y':
                $stringGuest = 'out guests';
                if ($arrayMemberAttendance['Guests'] > 0) {
                    if ($arrayMemberAttendance['Guests'] == 1) {
                        $stringGuest = ' 1 guest';
                    } else {
                        $stringGuest = ' ' . $arrayMemberAttendance['Guests'] . ' guests';
                    }
                }
                echo '<p class="entrybodybold"><font color="#00CC00">You are Attending with' . $stringGuest .  '. You can change your attendance by selecting one of the options below.</font></p>';

                break;

            case 'n':
                echo '<p class="entrybodybold"><font color="#FF8A14">You are NOT attending. You can change your attendance by selecting one of the options below.</font></p>';
                break;

            case 'p':
            default:
                echo '<p class="entrybodybold"><font color="#FF1F1F">You have not RSVP yet. Please select one of the following options to RSVP.</font></p>';
                break;
        }
    }
}
**** */


//------------------------------------------------------------------------------
// Function to show information about Member Attendance
//
//   $eventId = The Event Id in question
//   $arrayMemberAttendance = Array containing all information about the event
//   $isUpcoming = TRUE (Default) to allow users to submit form, FALSE otehrwise
//	returns echo Member Attendance Info
//------------------------------------------------------------------------------
/* ****
function showRSVPForm($eventId, $maxNumbGuests, $isUpcoming = TRUE) {

    if ($isUpcoming)
    {
        echo '<form id="rsvpF" name="rsvpF" method="POST" action="RSVPExec.php ">';
        echo    '<fieldset id="rsvpForm">';

        echo '<div align="center">
              <input type="submit" name="NoAttending" value="No Attending" style="width:170px"/>
              </div>';

        echo '<div align="center">
              <input type="submit" name="Attending0" value="Attending - No Guests" style="width:170px"/>
              </div>';

        for ($index = 0; $index < $maxNumbGuests; $index++)
        {
            if ($index == 0)
            {
                echo '<div align="center">
                     <input type="submit" name="Attending1" value="Attending - 1 Guest" style="width:170px"/>
                     </div>';
            }
            else
            {
                echo '<div align="center">
                     <input type="submit" name="Attending' . ($index+1) .'" value="Attending - ' . ($index+1) . ' Guests" style="width:170px"/>
                     </div>';
            }
        }

        echo '<input type="hidden" name="EventId" value="' . $eventId .'" />';
        
        echo    '</fieldset>';
        echo '</form>';
    }
    else
    {
        echo '<p class="entrybodybold"><font color="#FF1F1F">We are Sorry. This event is currently BLOCKED for RSVP. If you require further assistance, please contact one of the Excutives.</font></p>';
    }

}
**** */

function changeSQLDateEventDisplay($dateSql){
    //$dateConverted = DateTime::createFromFormat('Y-m-d', $dateSql);
    $pieceDate = explode('-',$dateSql);
    return date('l jS \of F\, Y',mktime(0, 0, 0, $pieceDate[1], $pieceDate[2], $pieceDate[0]));
}



function messageToRSVPorLogin($memberInfo){
    if(empty($memberInfo))
    {
        return '<a href="LogIn.php" title="Login">Login</a> to RSVP';
    }
    else
    {
        return 'Click <a href="RSVP.php" title="RSVP">here </a>to RSVP';
    }
}


function findImageNameExtensionEvent($eventId){
    
    $finalFile = UPLOADS_IMGEVENTS_DIR . 'default.png';
    $fileCreated = 0;
    
    $fileSearch =  UPLOADS_IMGEVENTS_DIR . $eventId . '.jpg';
    if (file_exists($fileSearch)) {
        if($fileCreated < filemtime($fileSearch))
        {
            $finalFile = $fileSearch;
            $fileCreated = filemtime($fileSearch);
        }
    }

    $fileSearch =  UPLOADS_IMGEVENTS_DIR . $eventId . '.jpeg';
    if (file_exists($fileSearch)) {
        if($fileCreated < filemtime($fileSearch))
        {
            $finalFile = $fileSearch;
            $fileCreated = filemtime($fileSearch);
        }
    }

    $fileSearch =  UPLOADS_IMGEVENTS_DIR . $eventId . '.gif';
    if (file_exists($fileSearch)) {
        if($fileCreated < filemtime($fileSearch))
        {
            $finalFile = $fileSearch;
            $fileCreated = filemtime($fileSearch);
        }
    }

    $fileSearch =  UPLOADS_IMGEVENTS_DIR . $eventId . '.png';
    if (file_exists($fileSearch)) {
        if($fileCreated < filemtime($fileSearch))
        {
            $finalFile = $fileSearch;
            $fileCreated = filemtime($fileSearch);
        }
    }

    return $finalFile;
}


function formatDescriptionEvent($descrip, $timingEvent='U')
{
    if(strcasecmp($timingEvent,'U') == 0 || strcasecmp($timingEvent,'D') == 0)
    {
        return nl2br($descrip);
    }
    else
    {
        return nl2br(splitLongTextMaxWords($descrip, MAX_NUM_WORDS_EVENTS_PAST_FUTURE)) ;
    }
}


function splitLongTextMaxWords($longText, $maxNumWords=50){
    $arr = preg_split("/[\s]+/", $longText,$maxNumWords+1);
    $arr = array_slice($arr,0,$maxNumWords);
    return join(' ',$arr);

}

?>
